package cn.com.thinvent.dispatch.service.biz.impl;

import cn.com.thinvent.dispatch.dao.ModelFiletagMapper;
import cn.com.thinvent.dispatch.dao.PdfFileMapper;
import cn.com.thinvent.dispatch.entity.ModelFiletag;
import cn.com.thinvent.dispatch.entity.file.filevo;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.service.biz.ModelFiletagService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class ModelFiletagImpl implements ModelFiletagService {
    @Resource
    private ModelFiletagMapper modelFiletagMapper;
    @Resource
    private PdfFileMapper pdfFileMapper;
    @Override
    public PagedGridResult<ModelFiletag> list(ModelFiletag modelFiletag, Integer pageNum, Integer pageSize) {
        PagedGridResult<ModelFiletag> pagedGridResult = new PagedGridResult<>();
        Integer pageBegin = (pageNum - 1) * pageSize;
        List<ModelFiletag> list = modelFiletagMapper.queryAllByLimit(modelFiletag, pageBegin, pageSize);
        Long count = modelFiletagMapper.count(modelFiletag);
        pagedGridResult.setRows(list);
        pagedGridResult.setRecords(count);
        pagedGridResult.setTotal((Integer.parseInt(count.toString()) + pageSize - 1 )/ pageSize);
        pagedGridResult.setPage(pageNum);
        return pagedGridResult;
    }

    @Override
    public ModelFiletag saveOrUpdate(ModelFiletag modelFiletag) {
        Date date = new Date();
        if(modelFiletag.getId() == null) {
            modelFiletag.setCreateTime(date);
            modelFiletag.setUpdateTime(date);
            modelFiletagMapper.insert(modelFiletag);
        } else {
            modelFiletag.setUpdateTime(date);
            modelFiletagMapper.update(modelFiletag);
        }
        return modelFiletag;
    }

    @Override
    public Integer delete(Integer id) {
        List<filevo> filevos = pdfFileMapper.listByTagid(id);
        if(filevos.isEmpty()){
            return modelFiletagMapper.deleteById(id);
        }else{
            return 0;
        }

    }
}
